rm(list = ls())
require(tidyverse)
require(ggridges)
library(openai)

setwd('D:/Dropbox/FED/FED/Paper/JOP/RR1_replication/')

load('./data/prepped_chunks_for_GPT.RData')

Sys.setenv(OPENAI_API_KEY = '') # Set openai API key here


# Functions
create_prompt <- function(chunk,direction = 'more') {
  res <- list(
    list(
      "role" = "system",
      "content" = "An aggressive communication style is a way of communicating with others than involves assertiveness, dominance, bluntness, verbal attacks, ignoring boundaires, hostility, lack of empathy, manipulation, and defensiveness."
    ),
    list(
      "role" = "user",
      "content" = stringr::str_c(
        'Please read the following conversations between a chair of the Federal Reserve and a member of Congress. Out of the two examples, which conversation is ',
        direction,' aggressive overall? Within the selected conversation, which speaker is more aggressive?\n\n',
        chunk)
    )
  )
  return(res)
}

clean_result <- function(result,direction = 'more') {
  res <- list(
    list(
      "role" = "user",
      "content" = stringr::str_c(
        result,
        '\n\nIn the above text, which conversation is described as ',direction,' polite? Return only the number of the conversation.')
    )
  )
  return(res)
}

submit_openai <- function(prompt, temperature = 0.2, n = 1) {
  res <- openai::create_chat_completion(model = "gpt-3.5-turbo",
                                        messages = prompt,
                                        temperature = temperature,
                                        n = n)
  Sys.sleep(1)
  res
}


# Creating random set of pairwise comparisons to feed to ChatGPT
set.seed(123)
chunks <- list()
counter <- 1
for(nConv in c(3:12)) {
  qntls <- quantile(toSample %>%
                      filter(n == nConv) %>% 
                      pull(nchars))
  
  for(q in 2:length(qntls)) {
    tmp <- toSample %>%
      filter(n == nConv,
             nchars < qntls[q],
             nchars > qntls[q-1])
    
    # test <- NULL
    for(i in 1:50) {
      tmp2 <- tmp %>%
        group_by(fed) %>%
        sample_n(size = 1) %>%
        ungroup() %>%
        sample_n(size = 2) %>%
        slice(sample(1:2,2))
      
      chunks[[counter]] <- list()
      chunks[[counter]]$chunk <- paste(paste0('Conversation ',1:2,':\n',
                                              tmp2 %>% pull(text),collapse = '\n\n'))
      chunks[[counter]]$srcs <- tmp2
      counter <- counter + 1
    }
  }
}


# Feeding the chunks into ChatGPT
res <- NULL
for(i in 1:length(chunks)) {
  cat('----------------------------\n',i,'\n----------------------------\n')
  for(d in c('more','less')) { # Phrasing of question (more or less aggressive)
    for(rev in c(T,F)) { # Is the first chunk Yellen or one of the other Fed chairs?
      # stop()
      if(rev) {
        torev <- str_split(chunks[[i]]$chunk,pattern = '(\n\nConversation 2:)')[[1]]
        chnk <- paste(paste0('Conversation 1:',torev[2]),
                      gsub('Conversation 1','Conversation 2',torev[1]),sep = '\n\n')
        srcs <- chunks[[i]]$srcs %>% slice(2,1)
      } else {
        chnk <- chunks[[i]]$chunk
        srcs <- chunks[[i]]$srcs
      }
      
      prompts <- create_prompt(chunk = chnk,direction = d)
      if(nchar(prompts[[2]]$content) > 10000) { next }
      
      Sys.sleep(2)
      system.time(openai_completions <- try(submit_openai(prompt = prompts,temperature = 0,n = 1)))
      
      while(class(openai_completions) == 'try-error') {
        Sys.sleep(5)
        system.time(openai_completions <- try(submit_openai(prompt = prompts,temperature = 0,n = 1)))
      }

      # openai_completions
      res <- res %>%
        bind_rows(srcs %>%
                    select(-docID,-chunkIndicator) %>%
                    mutate(id = row_number()) %>%
                    pivot_wider(names_from = id,values_from = c('date','n','text','fed','nchars','chamber')) %>%
                    mutate(explanation = openai_completions$choices$message.content,
                           direction = d,
                           reversed = rev))
    }
  }
}
